const app = getApp();
var openid = wx.getStorageSync('openid');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    hasMore: true,
    type: 1,//请求的模块1是我的赞2是我的评论3是我的内容
    subtitle: '',
    contentList: [],
    page: 1,
    total: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /*初始化列表数据*/
  initData: function () {
    this.showLoading();
    var that = this;
    var hasMore = true;
    //重置页码为1
    that.setData({
      page: 1,
    });
    wx.request({
      url: app.config.serverCfg.domain.myContent,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openid,
        page: that.data.page,
        type: that.data.type,
      },
      success: function (res) {
        if (res.data.data.length <= 20) {
          hasMore = false;
        }
        that.hideLoading();
        that.setData({
          contentList: res.data.data,
          hasMore: hasMore,
          total: res.data.total,
        })
      }
    });
  },
  /*加载更多*/
  loadMore() {
    var that = this;
    this.showLoading();
    var hasMore = true;
    wx.request({
      url: app.config.serverCfg.domain.myContent,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openid,
        page: ++that.data.page,
        type: that.data.type
      },
      success: function (res) {
        if (res.data.data.length == 0) {
          hasMore = false;
        }
        that.data.contentList = that.data.contentList.concat(res.data.data);
        that.hideLoading();
        that.setData({
          contentList: that.data.contentList,
          toatal: res.data.toatal,
          hasMore: hasMore,
        })
      }
    });
  },
  showLoading() {
    wx.showNavigationBarLoading();
    this.setData({
      subtitle: '加载中...',
      loading: true,
    });
  },
  hideLoading: function () {
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
    this.setData({
      loading: false
    });
  },
})